<template>
    <div class="pagebody">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 国家管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="search-box">
<el-form :inline="true">
                    <el-form-item label="国家名称">
                        <el-input v-model="searchSet.name" prefix-icon="el-icon-search" clearable placeholder="请输入国家名称"></el-input>
                    </el-form-item>
                    <el-form-item label="国家代号">
                        <el-input v-model="searchSet.code" prefix-icon="el-icon-search" clearable placeholder="请输入国家代号"></el-input>
                    </el-form-item>
                    <el-button type="primary" icon="el-icon-refresh" @click="handleSearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="handleAddNew">新建</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="handleImport">导入</el-button>
                </el-form>

            </div>
            <el-table :data="tableData" v-loading="tableLoading" border class="table" ref="mainTable" header-cell-class-name="table-header">
                <el-table-column prop="name" label="国家名称" align="center"></el-table-column>
                <el-table-column prop="code" label="国家编号" align="center"></el-table-column>

                <el-table-column label="操作" width="140" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="color-red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="sizes, total, prev, pager, next"
                    :current-page="query.index"
                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                    :page-size="query.pageSize"
                    :total="itemTotal"
                    @current-change="handlePageChange"
                    @size-change="handlePageSizeChange"
                ></el-pagination>
            </div>
        </div>

        <el-dialog :title="editIsNewAdd ? '新建国家' : '编辑国家'" v-dialogDrag center :visible.sync="editVisible" width="60%">
            <el-form ref="form" :model="form" :rules="formRules" label-width="120px">
                <el-form-item label="国家名称" prop="name">
                    <el-input
                        v-model="form.name"
                        placeholder="请输入国家名称"
                        maxlength="15"
                        show-word-limit
                        clearable
                        class="formInput"
                    ></el-input>
                </el-form-item>
                <el-form-item label="国家代号" prop="code">
                    <el-input
                        v-model="form.code"
                        placeholder="请输入国家代号"
                        maxlength="15"
                        show-word-limit
                        clearable
                        class="formInput"
                    ></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
                
            </span>
        </el-dialog>
        <el-dialog title="国家导入" v-dialogDrag center :visible.sync="importVisible" width="80%">
            <el-form ref="importform" :model="importform" label-width="120px">
                <el-form-item label="国家导入">
                    <div class="FlexRow">
                        <div>
                            <el-button type="primary" icon="el-icon-download" style="margin: 5px 10px 0 0" @click="downloadXlSX"
                                >下载国家导入模板</el-button
                            >
                        </div>
                        <nationFileUpload
                            style="margin: 5px 10px"
                            accept="xlsx/*"
                            ref="UploadImage"
                            :limit="1"
                            id="importformupload"
                            tip="只能上传xlsx文件"
                            v-on:update:excelData="UpdateExcel"
                        >
                        </nationFileUpload>
                    </div>

                    <el-table :data="ImportTableData" stripe border max-height="300" style="width: 100%">
                        <el-table-column v-for="(item, index) in ImportTableHeader" :key="index" :prop="String(index)" :label="item">
                        </el-table-column>
                    </el-table>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="importVisible = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import API from '../../../api/index'; //读入restfulAPI
import StringTool from '../../../utils/StringTool';
import JSTool from '../../../utils/JSTool';
import dataTable from './dataTable';
import nationFileUpload from '../../common/upload/nationFileUpload.vue';
export default {
    name: 'nationTable',
    mixins: [dataTable],
    components: {
        nationFileUpload
    },
    data() {
        return {
            searchSet: {
                name: '',
                code: ''
            },
            query: {
                status: 1,
                index: 1,
                pageSize: 10
            },
            editVisible: false,
            editIsNewAdd: false,
            form: {
                id: '',
                code: '',
                name: ''
            },
            formRules: {
                name: [{ required: true, message: '参数不能为空', trigger: 'change' }],
                code: [{ required: true, message: '参数不能为空', trigger: 'change' }]
            },
            importVisible: false,
            importform: {},
            ImportTableHeader: [],
            ImportTableData: []
        };
    },
    created() {
        this.init();
    },
    mounted() {
        // this.PreviewPhotoList = ["http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]
        // this.$refs.PreviewImage.clickHandler()
    },
    methods: {
        //页面初始化
        init() {
            this.getData();
        },

        getData() {
            
            this.tableLoading = true;
            var getQ = this.getQuery();
            API.StandardDoWith(API.Nation.Get(getQ), this, {
                success: (detail) => {
                    this.itemTotal = detail.count;
                    this.tableData = detail.list;
                },
                complete: () => {
                    this.tableLoading = false;
                }
            });
        },

        handleAddNew() {
            this.editVisible = true;
            this.editIsNewAdd = true;
            this.form = {
                id: '',
                code: '',
                name: ''
            };
        },

        // 编辑操作
        handleEdit(index, row) {
            this.form = JSTool.DeepClone(row);
            this.editIsNewAdd = false;
            this.editVisible = true;
        },

        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    API.StandardDoWith(API.Nation.Delete({ id: row.id }), this, {
                        success: (detail) => {
                            this.$message.success(`成功删除国家`);
                            this.getData();
                        }
                    });
                })
                .catch(() => {});
        },
        saveEdit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    let net = this.editIsNewAdd ? API.Nation.Post(this.form) : API.Nation.Put(this.form);
                    API.StandardDoWith(net, this, {
                        success: (detail) => {
                            this.editVisible = false;
                            this.$message.success(`成功提交代理`);
                            this.getData();
                        }
                    });
                } else {
                    this.$message.error(`表单验证失败，无法提交`);
                    return false;
                }
            });
        },
        handleImport() {
            this.importVisible = true;
            this.importform.data = [];
            this.ImportTableHeader = [];
            this.ImportTableData = [];
        },
        downloadXlSX() {
            window.location.href =  this.$store.state.baseURL + 'api/Nationfile';
        },
        UpdateExcel(excelData) {
            this.importform.data = excelData;
            this.ImportTableHeader = [];
            if (this.importform && this.importform.data && this.importform.data.length > 0) {
                this.ImportTableHeader = this.importform.data[0];
            }
            this.ImportTableData = [];
            if (this.importform && this.importform.data && this.importform.data.length > 1) {
                let list = [];
                this.importform.data.map((it) => {
                    list.push(it);
                });
                list.splice(0, 1);
                this.ImportTableData = list;
            }
        },
    }
};
</script>

<style scoped>
@import '../../../assets/css/dataTable.css';
</style>
